//
//  MBHeaderLinesImageContantView.m
//  MobellCloud_ios
//
//  Created by cherry pu on 2017/12/26.
//  Copyright © 2017年 zhangXin. All rights reserved.
//

#import "MBHeaderLinesImageContantView.h"

@interface MBHeaderLinesImageContantView ()
@property (nonatomic,assign)float imageWidth;
@property (nonatomic,strong)NSMutableArray* imageArray;
@property (nonatomic,strong)UIImage* defaultCover;

@end

@implementation MBHeaderLinesImageContantView

-(instancetype)initWithImageWidth:(CGFloat)imageWidth
{
    if (self = [super init]) {
        self.imageWidth = imageWidth;
        for (int i=0; i<9; i++) {
            UIImageView* imageView = [[UIImageView alloc]init];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.layer.masksToBounds = YES;
            imageView.image = self.defaultCover;
            imageView.tag = i+100;
            imageView.userInteractionEnabled = YES;
            UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleClickImageWithImageView:)];
            [imageView addGestureRecognizer:tap];
            imageView.hidden = YES;
            [self.imageArray addObject:imageView];
            [self addSubview:imageView];
            
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self).with.offset((self.imageWidth+[UIView lf_sizeFromIphone6:3])*(i/3));
                make.left.equalTo(self).with.offset((self.imageWidth+[UIView lf_sizeFromIphone6:3])*(i%3));
                make.size.mas_equalTo(CGSizeMake(self.imageWidth, self.imageWidth));
            }];
            
        }
        
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo((self.imageWidth+[UIView lf_sizeFromIphone6:3])*3);
            make.width.mas_equalTo(SCREEN_WIDTH-[UIView lf_sizeFromIphone6:20]);
        }];
    }
    return self;
}

#pragma mark - lazy load
-(NSMutableArray *)imageArray
{
    if (_imageArray == nil) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}

-(UIImage *)defaultCover
{
    if (_defaultCover == nil) {
        _defaultCover = [UIImage imageFromColor:COLOR_BG forSize:CGSizeMake(self.imageWidth, self.imageWidth) withCornerRadius:0];
    }
    return _defaultCover;
}


-(void)handleClickImageWithImageView:(UITapGestureRecognizer*)tapGesture
{
    UIView* view = tapGesture.view;
    self.imageIndex = (int)view.tag;
}

-(void)setImagePathArray:(NSArray *)imagePathArray
{
    _imagePathArray = imagePathArray;
    
    for (UIView* subView in self.subviews) {
        subView.hidden = YES;
    }
    
    if(!imagePathArray || imagePathArray.count == 0){
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }else{
        int line = (int)imagePathArray.count/3;
        if (imagePathArray.count % 3 != 0) {
            line += 1;
        }
        
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(line*(self.imageWidth+[UIView lf_sizeFromIphone6:3]));
        }];
        
    }
    
    for (int i =0; i<imagePathArray.count; i++) {
        NSString* imageUrl = imagePathArray[i];
        
        if (![imageUrl containsString:@"http"]) {
            imageUrl = [NSString stringWithFormat:@"%@%@",BASE_IMAGE_URL,imageUrl];
        }

        UIImageView* imageView = self.imageArray[i];
        imageView.hidden = NO;
        imageView.image = self.defaultCover;
        if (imageUrl && ![imageUrl isEqualToString:@""]) {
            UIImage* coverImage = [[MBCacheService sharedInstance]fetchBannerImageWithUrl:imageUrl];

            if (coverImage) {
                imageView.image = coverImage;
            }else {
                [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:self.defaultCover completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                    if (image) {
                        imageView.image = image;

                        [[MBCacheService sharedInstance]cacheBannerImageWithUrl:imageUrl andImage:image];
                        [[SDImageCache sharedImageCache] clearMemory];
                    }
                }];
            }
        }
    }
    
}


@end
